﻿using System;
using EOSCRM.Dao;
using EOSCRM.Util;
using EOSCRM.Web.Common;
using EOSCRM.Web.Shared;

namespace EOSCRM.Web.Forms.Group
{
    public partial class Creation : Authentication
    {
        #region Variable(s)

        private readonly GroupDao groupDao = new GroupDao();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                PrepareUI();
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void PrepareUI()
        {
            UcGroupUI.LoginInfo = LoginInfo;
        }

        #region Startup script registeration
        private void ShowError(string message)
        {
            var master = (EOS) Page.Master;
            if (master != null) master.ShowError(message);
        }

        private void ShowInfor(string message)
        {
            var master = (EOS) Page.Master;
            if (master != null) master.ShowInfor(message);
        }

        private void CloseWaitingDialog()
        {
            var master = (EOS) Page.Master;
            if (master != null) master.CloseWaitingDialog();
        }

        #endregion

        private void DoSave()
        {
            try
            {
                // Check unique name
                if (groupDao.IsNameDuplicated(null, UcGroupUI.Group.Name, false))
                {
                    CloseWaitingDialog();
                    ShowError("Tên nhóm đã tồn tại");
                    return;
                }

                var msg = groupDao.Insert(UcGroupUI.Group);

                if (msg != null)
                {
                    if (msg.MsgType != MessageType.Error)
                    {
                        CloseWaitingDialog();
                        ShowInfor(ResourceLabel.Get(msg));
                        Response.Redirect(WebUrlConstants.GROUP_LIST, false);
                        return;
                    }

                    CloseWaitingDialog();
                    ShowError(ResourceLabel.Get(msg));
                    return;
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void DoCancel()
        {
            Response.Redirect(WebUrlConstants.GROUP_LIST, false);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            DoCancel();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DoSave();
        }

        protected void btnCancelBottom_Click(object sender, EventArgs e)
        {
            DoCancel();
        }

        protected void btnSaveBottom_Click(object sender, EventArgs e)
        {
            DoSave();
        }
    }
}